package com.xiaoq.sites.repository.impl;

import java.util.Date;
import java.util.List;

import org.apache.commons.lang3.StringUtils;
import org.mongodb.morphia.Datastore;
import org.mongodb.morphia.query.Query;
import org.mongodb.morphia.query.UpdateOperations;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Repository;

import com.xiaoq.coms.util.IdGenerator;
import com.xiaoq.sites.entity.SitesRouteEntity;
import com.xiaoq.sites.repository.ISitesRoutePointRepository;
import com.xiaoq.sites.repository.ISitesRouteRepository;

@Repository
public class SitesRouteRepositoryImpl implements ISitesRouteRepository {
	
	@Autowired
	private Datastore datastore;
	
	@Autowired
	private ISitesRoutePointRepository iSitesRoutePointRepository;
	
	@Autowired
	private IdGenerator idGenerator;

	@Override
	public SitesRouteEntity findById(String id) {
		return datastore.get(SitesRouteEntity.class, id);
	}

	@Override
	public List<SitesRouteEntity> findSitesRouteListBySitesId(String sitesId) {
		Query<SitesRouteEntity> sitesRouteQuery = datastore.find(SitesRouteEntity.class);
		sitesRouteQuery.field("sitesId").equal(sitesId);
		return sitesRouteQuery.asList();
	}

	@Override
	public boolean sitesRouteAdd(SitesRouteEntity sitesRouteEntity) {
		if(sitesRouteEntity!=null){
		String sitesId = sitesRouteEntity.getSitesId();
		String name = sitesRouteEntity.getName();
		Query<SitesRouteEntity>sitesRouteQueryExsiting=datastore.find(SitesRouteEntity.class);
		List <SitesRouteEntity> sitesRouteEntityList = sitesRouteQueryExsiting.field("sitesId").equal(sitesId).field("name").equal(name).asList();
		if(sitesRouteEntityList.isEmpty()){
			sitesRouteEntity.setId(idGenerator.nextId());
			sitesRouteEntity.setCreatedAt(System.currentTimeMillis());
			sitesRouteEntity.setUpdatedAt(System.currentTimeMillis());
			datastore.save(sitesRouteEntity);
			return true;
		}else{
			return false;
		}
		
			
			
		}
		
		return false;
	}

	@Override
	public boolean delSitesRoute(String routeId) {
		boolean pointRouteResult;
		if(StringUtils.isNotBlank(routeId)){
			
			datastore.delete(datastore.get(SitesRouteEntity.class,routeId));
			pointRouteResult = iSitesRoutePointRepository.deleteRoutePointByRouidPointId(routeId);
			if(pointRouteResult){
				return true;
			
			}
		
		}
		
		
		return true;
	}

	@Override
	public boolean updateSitesRoute(SitesRouteEntity sitesRoute) {
		String sitesRouteId  = sitesRoute.getId();
		Query<SitesRouteEntity> routeQuery = datastore.createQuery(SitesRouteEntity.class).field("_id").equal(sitesRouteId);
		UpdateOperations<SitesRouteEntity> sitesRouteUpdate = datastore.createUpdateOperations(SitesRouteEntity.class);
		if(sitesRoute.getName()!=null)
			sitesRouteUpdate = sitesRouteUpdate.set("name",sitesRoute.getName());
		if(sitesRoute.getShortName()!=null)
			sitesRouteUpdate = sitesRouteUpdate.set("shortName",sitesRoute.getShortName());
		if(sitesRoute.getIntroduction()!=null)
			sitesRouteUpdate = sitesRouteUpdate.set("introduction",sitesRoute.getIntroduction());
		if(sitesRoute.getTags()!=null)
			sitesRouteUpdate = sitesRouteUpdate.set("tags",sitesRoute.getTags());
		if(sitesRoute.getMemo()!=null)
			sitesRouteUpdate = sitesRouteUpdate.set("memo",sitesRoute.getMemo());
		if(sitesRoute.getAvgDistance()!=null)
			sitesRouteUpdate = sitesRouteUpdate.set("avgDistance",sitesRoute.getAvgDistance());
		if(sitesRoute.getAvgTime()!=null)
			sitesRouteUpdate = sitesRouteUpdate.set("avgTime",sitesRoute.getAvgTime());
		if(sitesRoute.getAvgKaluli()!=null)
			sitesRouteUpdate = sitesRouteUpdate.set("avgKaluli",sitesRoute.getAvgKaluli());
		if(sitesRoute.getSitesId()!=null)
			sitesRouteUpdate = sitesRouteUpdate.set("sitesId", sitesRoute.getSitesId());
		if(sitesRoute.getMoveMethod()!=null)
			sitesRouteUpdate = sitesRouteUpdate.set("moveMethod",sitesRoute.getMoveMethod());
		if(sitesRoute.getNotice()!=null)
			sitesRouteUpdate = sitesRouteUpdate.set("notice",sitesRoute.getNotice());
		if(sitesRoute.getStartLongitude()!=null)
			sitesRouteUpdate = sitesRouteUpdate.set("startLongitude",sitesRoute.getStartLongitude());
		if(sitesRoute.getStartLatitude()!=null)
			sitesRouteUpdate = sitesRouteUpdate.set("startLatitude",sitesRoute.getStartLatitude());
		if(sitesRoute.getEndLongitude()!=null)
			sitesRouteUpdate = sitesRouteUpdate.set("endLongitude",sitesRoute.getEndLongitude());
		if(sitesRoute.getEndLatitude()!=null)
			sitesRouteUpdate = sitesRouteUpdate.set("endLatitude",sitesRoute.getEndLatitude());
		sitesRouteUpdate = sitesRouteUpdate.set("updatedAt", System.currentTimeMillis());;
															
		
		datastore.update(routeQuery, sitesRouteUpdate);													
		return true;
	}

	

}
